package com.atguigu.ggkt.result;

import lombok.Data;

/**
 * 全局统一返回结果类
 */
@Data
public class Result<T> {
    /** 返回码 */
    private Integer code;
    /** 返回消息 */
    private String message;
    /** 返回数据 */
    private T data;



    /**
     * 操作成功
     */
    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<T>();
        if (data != null) {
            result.setData(data);
        }
        result.setCode(20000);
        result.setMessage("成功");
        return result;
    }

    /**
     * 操作失败
     */
    public static <T> Result<T> fail(T data) {
        Result<T> result = new Result<T>();
        if (data != null) {
            result.setData(data);
        }
        result.setCode(20001);
        result.setMessage("失败");
        return result;
    }

    //方法可调用：对message和code修改
    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }
}
